<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跑马灯轮播图特效</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .carousel {
            width: 650px;
            height: 360px;
            border: 1px solid #000;
            margin: 50px auto;
            position: relative;
            overflow: hidden;
        }

        .carousel ul {
            list-style: none;
            width: 6000px;
            position: relative;
            left: 0;
            transition: left .5s ease 0s;
        }

        .carousel ul li {
            float: left;
        }

        .carousel .leftbtn {
            position: absolute;
            left: 20px;
            width: 50px;
            height: 50px;
            background-color: orange;
            border-radius: 50%;
            top: 50%;
            margin-top: -25px;
        }

        .carousel .rightbtn {
            position: absolute;
            right: 20px;
            width: 50px;
            height: 50px;
            background-color: orange;
            border-radius: 50%;
            top: 50%;
            margin-top: -25px;
        }
    </style>
</head>

<body>
    <div class="carousel">
        <ul id="list">
            <li>
                <img src="images/beijing/0.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/1.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/2.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/3.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/4.jpg" alt="">
            </li>
        </ul>
        <a href="javascript:;" class="leftbtn" id="leftbtn"></a>
        <a href="javascript:;" class="rightbtn" id="rightbtn"></a>
    </div>
    <script>
        //得到按钮和ul,ul整体进行运动
        var leftbtn = document.getElementById('leftbtn')
        var rightbtn = document.getElementById('rightbtn')
        var list = document.getElementById('list')

        //克隆第一张图片
        var cloneli = list.firstElementChild.cloneNode(true);
        list.appendChild(cloneli)

        //当前ul显示到第几张了,从0开始数
        var idx = 0;


        //节流锁
        var lock = true;
        //监听
        rightbtn.onclick = function () {
            if (!lock) return;

            lock = false;
            //给list加过渡
            list.style.transition = 'left .5s ease 0s'
            idx++;
            if (idx > 4) {
                //设置一个延时器，功能将ul瞬间拉回0的位置
                setTimeout(function () {
                    //取消过渡
                    list.style.transition = 'none';
                    list.style.left = 0;
                    idx = 0;
                }, 500)
            }
            list.style.left = -idx * 650 + 'px'
            setTimeout(function () {
                lock = true;
            }, 500)
        }

        leftbtn.onclick = function () {
            if (!lock) return;

            lock = false;
            //判断是不是第0张，如果是，就要瞬间用假的替换真的
            if (idx == 0) {
                //取消过渡
                list.style.transition = 'none';
                list.style.left = -5 * 650 + 'px';
                setTimeout(() => {
                    list.style.transition = 'left .5s ease 0s'
                    idx = 4;
                    list.style.left = -idx * 650 + 'px'
                }, 5);
            } else {
                idx--;
                list.style.left = -idx * 650 + 'px'
            }
            setTimeout(function () {
                lock = true;
            }, 500)
        }
    </script>
</body>

</html>